﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;

namespace WallPapper
{
    public partial class FormSetting : Form
    {

        public FormPapper vWall;


        public FormSetting()
        {
            InitializeComponent();
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "config.txt"))
            {
                try
                {
                    string[] array = File.ReadAllText("config.txt").Split('|');
                    vWall = new FormPapper(int.Parse(array[0]), array[1]);
                }
                catch (Exception)
                {
                    vWall = new FormPapper(0, AppDomain.CurrentDomain.BaseDirectory + "demo.mp4");
                }
            }
            else
            {
                vWall =  new FormPapper(0, AppDomain.CurrentDomain.BaseDirectory + "demo.mp4"); ;
            }

            vWall.Show();
        }

        private void FormSetting_FormClosed(object sender, FormClosedEventArgs e)
        {
            File.WriteAllText("config.txt", vWall.model + "|" + vWall.path);
            Process.GetCurrentProcess().Kill();
        }

        private void btn_selectPath(object sender, EventArgs e)
        {
            Control ctrl = sender as Control;
            int model = int.Parse(ctrl.Tag.ToString());
            System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                (Controls["textBox" + (model + 1)] as Control).Text = open.FileName;
            }
        }



        private void btn_Change(object sender, EventArgs e)
        {
            Control ctrl = sender as Control;
            int model = int.Parse(ctrl.Tag.ToString());
            string text = (Controls["textBox" + (model + 1)] as Control).Text;
            if (model != vWall.model)
            {
                vWall.Dispose();
                vWall = new FormPapper(model, text);
                vWall.Show();
            }
            else
            {
                vWall.path = text;
            }

        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            IntPtr trayHwnd = Win32API.FindWindow("Shell_TrayWnd", null);
            if (trayHwnd != IntPtr.Zero)
            {
                Win32API.ShowWindow(trayHwnd, 0);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            IntPtr trayHwnd = Win32API.FindWindow("Shell_TrayWnd", null);
            if (trayHwnd != IntPtr.Zero)
            {
                Win32API.ShowWindow(trayHwnd, 1);
            }
        }
    }
}
